using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using EcsSiteDSTableAdapters;

/// <summary>
/// Summary description for NewsBLL
/// </summary>
namespace SiteECSWeb
{
    [DataObject]
    public class NewsBLL
    {
        #region ClassConstructor
        public NewsBLL()
        {

        }
        #endregion

        private NewsTableAdapter _NewsAdapter = null;

        protected NewsTableAdapter NewsAdapter
        {
            get
            {
                if (_NewsAdapter == null)
                {
                    _NewsAdapter = new NewsTableAdapter();
                }
                return _NewsAdapter;
            }
        }

        #region SelectableTable
        public int GetTotalNumberOfNews()
        {
            Nullable<int> totalNull = (Nullable<int>)NewsAdapter.TotalNumberOfNews();
            return totalNull.GetValueOrDefault();
        }
        public int GetTotalNumberOfNews(string FilterStr)
        {
            if (string.IsNullOrEmpty(FilterStr))
            {
                return GetTotalNumberOfNews();
            }
            else
            {
                Nullable<int> totalNull = (Nullable<int>)NewsAdapter.
                TotalNumberOfNewsByFilterExp(FilterStr);
                return totalNull.GetValueOrDefault();
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public EcsSiteDS.NewsDataTable GetALLNewsData()
        {
            return NewsAdapter.GetNewsRowRankData();
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.NewsDataTable GetNewsDataByNewsID(int News_id)
        {
            if (News_id == 0)
            {
                return NewsAdapter.GetNewsRowRankData();
            }
            else
            {
                return NewsAdapter.GetNewsDataByNews_id(News_id);
            }
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.NewsDataTable GetNewsDataWithTopFiveRankRows()
        {
            return NewsAdapter.GetNewsDataTopRowRank();
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.NewsDataTable GetNewsByNewsNmArFrstLetter(string News_nm ,int TopCount)
        {
            return NewsAdapter.
                GetNewsDataByNewsNmArFrstLetter(News_nm , TopCount);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.NewsDataTable GetNewsByRowRank(int RankID)
        {
            return NewsAdapter.GetNewsDataByRowRank(RankID);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.NewsDataTable GetNewsRowNumByMontYearhNum(int MonthNum, int YearNum)
        {
            return NewsAdapter.GetNewsDataByMontYearh(MonthNum, YearNum);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.NewsDataTable GetNewsDataByCountryCode(string Cuntry_un2)
        {
            if (string.IsNullOrEmpty(Cuntry_un2))
            {
                return NewsAdapter.GetNewsRowRankData();
            }
            else
            {
                return NewsAdapter.GetNewsDataByCountryCode(Cuntry_un2);
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.NewsDataTable GetNewsByGridPagesAndFilterExpression(int startRowIndex,
            int maximumRows, string sortExpression)
        {
            return NewsAdapter.GetNewsDataByGridPages(startRowIndex, maximumRows, sortExpression);
            //    EcsSiteDS.ExpOpprtintyDataTable ExpOppr = GetAllExportOpp();
            //    PagedDataSource pagedData = new PagedDataSource();
            //    pagedData.DataSource = ExpOppr.Rows;
            //    pagedData.AllowPaging = true;
            //    pagedData.CurrentPageIndex = pageIndex;
            //    pagedData.PageSize = pageSize;
            //    return pagedData;
        }


        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.NewsDataTable GetNewsByGridPagesAndFilterExpression(int startRowIndex, int maximumRows,
            string FilterStr, string sortExpression)
        {
            if (string.IsNullOrEmpty(FilterStr))
            {
                return
                    GetNewsByGridPagesAndFilterExpression(startRowIndex, maximumRows, sortExpression);
            }
            else
            {
                return NewsAdapter.GetNewsDataByGridPagesFilterExp
                    (startRowIndex, maximumRows, FilterStr, sortExpression);
            }
        }
        //[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        //public EcsSiteDS.NewsDataTable GetNewsDataByNewsType(string News_type)
        //{
        //    if (string.IsNullOrEmpty(News_type))
        //    {
        //        return NewsAdapter.GetNewsRowRankData();
        //    }
        //    else
        //    {
        //        return NewsAdapter.GetNewsDataByType(News_type);
        //    }
        //}
        #endregion

        #region InsertableTable
        [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public int AddNews(string News_nm, string News_surce, string News_descr, string News_type,
                              DateTime News_date, DateTime? PeriodDate, string News_link,
                              bool? Publishing, string Cuntry_un2)
        {

            EcsSiteDS.NewsDataTable Newstable = new EcsSiteDS.NewsDataTable();
            EcsSiteDS.NewsRow Newsrow = Newstable.NewNewsRow();

            if (PeriodDate == null) Newsrow.SetPeriodDateNull(); else Newsrow.PeriodDate = PeriodDate.Value;
            if (Publishing == null) Newsrow.SetPublishingNull(); else Newsrow.Publishing = Publishing.Value;
            if (News_descr == null) Newsrow.SetNews_descrNull(); else Newsrow.News_descr = News_descr;
            if (News_link == null) Newsrow.SetNews_linkNull(); else Newsrow.News_link = News_link;
            if (News_type == null) Newsrow.SetNews_typeNull(); else Newsrow.News_type = News_type;

            Newsrow.News_nm = News_nm;
            Newsrow.News_surce = News_surce;

            Newsrow.News_date = News_date;
            Newsrow.Cuntry_un2 = Cuntry_un2;

            Newstable.AddNewsRow(Newsrow);

            int rowsAffected = NewsAdapter.Update(Newstable);

            return (rowsAffected == 1) ? Newsrow.News_id : -1;
        }

        #endregion

        #region UpdatableTable
        [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public bool UpdateNews(string News_nm, string News_surce, string News_descr,
            string News_type, System.DateTime News_date, DateTime? PeriodDate, string News_link,
            bool? Publishing, string Cuntry_un2, int News_id)
        {
            EcsSiteDS.NewsDataTable Newstable = NewsAdapter.GetNewsDataByNews_id(News_id);

            if (Newstable.Count == 0)
            {
                return false;
            }
            EcsSiteDS.NewsRow NewsRow = Newstable[0];

            #region CheckTheNullValues

            if (PeriodDate == null)
            {
                NewsRow.SetPeriodDateNull();
            }
            else
            {
                NewsRow.PeriodDate = PeriodDate.Value;
            }

            if (Publishing == null)
            {
                NewsRow.SetPublishingNull();
            }
            else
            {
                NewsRow.Publishing = Publishing.Value;
            }
            if (string.IsNullOrEmpty(News_descr)) NewsRow.SetNews_descrNull(); else NewsRow.News_descr = News_descr;
            if (string.IsNullOrEmpty(News_link)) NewsRow.SetNews_linkNull(); else NewsRow.News_link = News_link;
            if (string.IsNullOrEmpty(News_type)) NewsRow.SetNews_typeNull(); else NewsRow.News_type = News_type;
            #endregion

            NewsRow.News_nm = News_nm;
            NewsRow.News_surce = News_surce;
            NewsRow.News_date = News_date;
            NewsRow.Cuntry_un2 = Cuntry_un2;


            int roweffected = NewsAdapter.Update(NewsRow);
            return roweffected == 1;
        }
        #endregion

        #region DeletableTable
        [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public bool DeleteNews(int News_id)
        {
            EcsSiteDS.NewsDataTable Newstable = NewsAdapter.GetNewsDataByNews_id(News_id);
            if (Newstable.Count == 0)
            {
                return false;
            }
            EcsSiteDS.NewsRow NewsRow = Newstable[0];
            if (!NewsRow.IsNews_linkNull())
            {
                System.IO.File.Delete
              (HttpContext.Current.Server.MapPath(Globals.Settings.UploadInfo.RootAttachFolder
                         + NewsRow.News_link));
            }
          
            int rowsAffected = NewsAdapter.Delete(News_id);
            return rowsAffected == 1;
        }
        #endregion
    }
}
